/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.admin;

import hibernate.dao.LoginDAO;
import hibernate.dao.RoleDAO;
import hibernate.dao.StaffDAO;
import hibernate.entity.Login;
import hibernate.entity.Staff;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.hibernate.Transaction;
import util.RandomGenerator;

/**
 *
 * @author NDH
 */
@ManagedBean(name = "staffManager")
@RequestScoped
public class StaffManager extends AbstractAdminBean {

    /**
     * Creates a new instance of StaffManager
     */
    List<Staff> listStaff;
    StaffDAO sdao = new StaffDAO();
    LoginDAO lgDao = new LoginDAO();
    List<String> allEducation;
    List<String> allPro;
    String education;
    String professional;
    String staffName;
    String birthday;
    String username;
    Staff currStaff = new Staff();

    public Staff getCurrStaff() {
        return currStaff;
    }

    public void setCurrStaff(Staff currStaff) {
        this.currStaff = currStaff;
    }
    int STAFF_ROLE_ID = 2;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getStaffName() {
        return staffName;
    }

    public void setStaffName(String staffName) {
        this.staffName = staffName;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public String getProfessional() {
        return professional;
    }

    public void setProfessional(String professional) {
        this.professional = professional;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    String email;

    public List<String> getAllEducation() {
        allEducation = new ArrayList<String>();
        allEducation.add("MBT");
        allEducation.add("Diploma");
        allEducation.add("PHD");
        return allEducation;
    }

    public void setAllEducation(List<String> allEducation) {
        this.allEducation = allEducation;
    }

    public List<String> getAllPro() {
        allPro = new ArrayList<String>();
        allPro.add("IT");
        allPro.add("Marketting");
        allPro.add("Sale");
        return allPro;
    }

    public void setAllPro(List<String> allPro) {
        this.allPro = allPro;
    }

    public List<Staff> getListStaff() {
        listStaff = sdao.readAll();
        return listStaff;
    }

    public void setListStaff(List<Staff> listStaff) {
        this.listStaff = listStaff;
    }

    public StaffManager() {
    }

    public String delete(Staff s) {
        try {
            sdao.delete(s);
            hbTransaction.commit();
        } catch (Exception e) {
        }
        return "list";
    }

    public String edit(Staff s) {
        currStaff = s;
        return "edit";
    }

    public String docreate() {
        RoleDAO rDao = new RoleDAO();
            String random = RandomGenerator.randomString();
//            Login lg = new Login(username, random, rDao.readByRoleName("staff"));
//            tx = ss.beginTransaction();
//            try {
//                if (lgDao.create(lg) > 0) {
//                    Staff s = new Staff(username, professional, education, email);
//                    s.setLogins(lg);
//                    s.setLoginId(lg.getAccId());
//                    if (sdao.create(s) > 0) {
//                        tx.commit();
//                        return "list";
//                    } else {
//                        tx.rollback();
//                    }
//                }
//
//            } catch (Exception e) {
//            tx.rollback();
//            }
            return "error";
//        }
       

    }

    public String doupdate() {
        try {
            if (sdao.update(currStaff) > 0) {
                showPrimeMessage("Success", "Staff updated");
            }
        } catch (Exception e) {
        }
        return "list";
    }
}
